package com.lcy.demo;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.Version;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 娄存银 on 2017/4/8.
 */
public class CreateDoc {
    private Configuration configuration;

    public CreateDoc() {
        configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");
    }

    public void create(){
        Map<String,Object> map = new HashMap<>();
        map.put("name","张三");
        map.put("age","16");
        map.put("time","2017-4-8");

        configuration.setClassLoaderForTemplateLoading(this.getClass().getClassLoader(),"/");
        try {
            Template template = configuration.getTemplate("template.xml");
            File outFile = new File("D:\\test.doc");
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
            template.process(map,out);
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        CreateDoc doc = new CreateDoc();
        doc.create();
    }
}
